package com.mz.mattress.admin.api.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.DeviceQueryReq;
import com.mz.mattress.admin.api.resp.SaveDeviceResp;
import com.mz.mattress.admin.service.DeviceService;
import com.mz.mattress.api.common.Constants;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Device;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping(Constants.ADMIN_PATH + "device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/listPage")
    public Result<IPage<Device>> listPage(DeviceQueryReq req) {
        return deviceService.listPage(req);
    }

    @GetMapping(value = "/{id}")
    public Result<Device> detail(@PathVariable Integer id) {
        return deviceService.getById(id);
    }

    @GetMapping(value = "/list")
    public Result<List<Device>> list(@RequestParam Integer merchantId) {
        return deviceService.list(merchantId);
    }

    @PostMapping(value = "/bind")
    public Result bind(@RequestParam Integer merchantId, @RequestParam String deviceCode) {
        return deviceService.bind(merchantId, deviceCode);
    }

    @PostMapping(value = "/unbind")
    public Result unbind(@RequestParam Integer id) {
        return deviceService.unbind(id);
    }

    @PostMapping(value = "/save")
    public Result<SaveDeviceResp> save(@RequestParam String content) {
        return deviceService.save(content);
    }

    @PutMapping
    public Result update(@RequestBody Device device) {
        return deviceService.updateById(device);
    }

    @PostMapping(value = "/updateStatus")
    public Result bind(@RequestParam Integer id, @RequestParam Integer status) {
        return deviceService.updateStatus(id, status);
    }

}
